package com.qymerge.manage.base.service.impl;

import com.qymerge.manage.base.dao.ITempMsgDao;
import com.qymerge.manage.base.dto.TempMsgDto;
import com.qymerge.manage.base.dto.export.TempMsgExportDTO;
import com.qymerge.manage.base.ro.TempMsgRO;
import com.qymerge.manage.base.service.ITempMsgService;
import com.qymerge.manage.core.bean.ActionResultRO;
import com.qymerge.manage.core.bean.DataTableRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.utils.ReadExcelUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *<p>Title: TempMsgServiceImpl</p>
 *<p>Description: H5预约</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19
 *@Time 18:18
 */
@Service
public class TempMsgServiceImpl implements ITempMsgService {

    @Autowired
    private ITempMsgDao tempMsgDao;

    /**
     * 查询
     *
     * @param ro
     * @param pageRequest
     * @return
     */
    @Override
    public DataTableRO list(TempMsgDto ro, PageRequest pageRequest) {
        List list = tempMsgDao.list(ro, pageRequest);
        DataTableRO dataTableRO = new DataTableRO();
        int allCount = tempMsgDao.total(ro);
        int pageSize = pageRequest.getSize();
        dataTableRO.setAll_count(allCount);
        dataTableRO.setList(list);
        dataTableRO.setCurrent_page(pageRequest.getPage());
        dataTableRO.setPage_size(pageSize);
        int allPage = (allCount % pageSize == 0 ? allCount / pageSize : allCount / pageSize + 1);
        dataTableRO.setAll_page(allPage);
        return dataTableRO;
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public TempMsgRO detail(Long id) {
        return tempMsgDao.findOne(id);
    }

    /**
     * 新增
     *
     * @param ro
     * @return
     */
    @Override
    public ActionResultRO save(TempMsgRO ro) {
        int rn = 0;
        if (ro.getTempmsgid() == 0) {
            rn = tempMsgDao.install(ro);
        } else {
            rn = tempMsgDao.update(ro);
        }
        return new ActionResultRO(rn, ro.getTempmsgid() == 0 ? "保存成功" : "修改成功", ro.getTempmsgid() == 0 ? "保存失败" : "修改失败");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public ActionResultRO delete(Long[] ids) {
        int rn = tempMsgDao.remove(Arrays.asList(ids));
        return new ActionResultRO(true, "删除" + rn +"成功", "删除" + rn +"失败");
    }

    /**
     *
     * @param multipartFile
     * @return
     */
    @Override
    public ActionResultRO importData(MultipartFile multipartFile, int flag) {
        StringBuffer tip = new StringBuffer("导入结果：<br/>");
        ReadExcelUtil readExcelUtil = new ReadExcelUtil();
        List<String[][]> datas = null;
        try {
            datas = readExcelUtil.readByStream(multipartFile.getInputStream(), 2, multipartFile.getOriginalFilename());
            if (org.springframework.util.CollectionUtils.isEmpty(datas)) {
                return new ActionResultRO(false, "execl数据为空");
            }
            String[][] data = datas.get(0);

            long id= 0L;
            String username= null;
            String mobile= null;
            String hxcode= null;
            int isuse= 0;
            String hxdate= null;
            String createdate= null;

            TempMsgRO tempMsgRO = null;
            int rowNum = 1;        // 记录当前行
            int successNum = 0;    // 记录新增或者更新记录数
            for (String[] row : data) {
                rowNum++;

                id = Long.valueOf(row[0]);
                username= row[1];
                mobile= row[2];
                hxcode= row[3];
                if (StringUtils.equals("是", row[4])) {
                    isuse = 1;
                }
                hxdate= row[5];
                createdate= row[6];

                if (StringUtils.isEmpty(id + "")
                        || StringUtils.isEmpty(username)) {
                    tip.append("第").append(rowNum).append("行：部分数据为空，不予新增<br/>");
                    continue;
                }

                tempMsgRO = new TempMsgRO(id, username, mobile, hxcode, isuse, hxdate, createdate);

                if (flag == 1) {
                    tempMsgDao.install(tempMsgRO);
                    successNum++;
                } else if (flag == 0) {
                    tempMsgDao.update(tempMsgRO);
                    successNum++;
                }

            }
            tip.append("成功"+ (flag == 1 ? "新增":"更新") +"记录：").append(successNum).append("条");
        } catch (IOException e) {
            e.printStackTrace();
            return new ActionResultRO(false, "导入失败");
        }

        return new ActionResultRO(true, tip.toString());
    }

    /**
     * 导出表格
     *
     * @param ro
     * @return
     */
    @Override
    public List<TempMsgExportDTO> exportData(TempMsgDto ro) {
        List<TempMsgExportDTO> list = new ArrayList<TempMsgExportDTO>();
        TempMsgExportDTO dto = null;

        List<TempMsgRO> cardROS = tempMsgDao.findAll(ro);
        if (CollectionUtils.isNotEmpty(cardROS)) {
            for (TempMsgRO tempMsgRO : cardROS) {
                dto = new TempMsgExportDTO();

                dto.setId(tempMsgRO.getTempmsgid());
                dto.setUsername(tempMsgRO.getUsername());
                dto.setMobile(tempMsgRO.getMobile());
                dto.setHxcode(tempMsgRO.getHxcode());
                dto.setIsuse(tempMsgRO.getIsuse() == 1 ? "是" : "否");
                dto.setHxdate(tempMsgRO.getHxdate());
                dto.setCreatedate(tempMsgRO.getCreatedate());

                list.add(dto);
            }
        }

        return list;
    }

    @Override
    public ActionResultRO updateIsuse(long id, int isuse, String message) {
        int rn = tempMsgDao.updateIsuse(isuse);
        return new ActionResultRO(rn, message + "成功", message + "失败");
    }


}
